//
//  PathView.m
//  CircleGradientLayer
//
//  Created by Dinotech on 16/1/6.
//  Copyright © 2016年 Dinotech. All rights reserved.
//

#import "PathView.h"

#import <QuartzCore/QuartzCore.h>
#define RYUIColorWithRGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
@implementation PathView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        
    }
    return self;
}

- (void)gradentWith:(CGRect)frame startAngle:(float)start endAngle:(float)end startColor:(UIColor *)color1 endColor:(UIColor *)color2 {
    [self.layer removeAllAnimations];
    for (NSInteger i=self.layer.sublayers.count-1; i>=0; i--) {
        [self.layer.sublayers[i] removeFromSuperlayer];
    }
    float length = 4;
    //设置贝塞尔曲线
//    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0) radius:self.frame.size.width/2.0 - 10 startAngle:- M_PI_2 endAngle:-M_PI_2 + M_PI * 2 clockwise:YES];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(frame.size.width/2, frame.size.height/2) radius:(frame.size.width-PROGRESS_LINE_WIDTH)/2-length/2 startAngle:degressToRadius(-start) endAngle:degressToRadius(end) clockwise:YES];
    //根据原来的frame 进行进度渐变
//    CGRect inset = CGRectInset(self.bounds, 20, 20);
//    CGRect inster = CGRectIntegral(inset);
//    CGRect rir2 = CGRectUnion(inset, self.bounds);
//    NSLog(@"------frame1 = %@",NSStringFromCGRect(self.bounds));
//    NSLog(@"------frame2 = %@",NSStringFromCGRect(inset));
//    NSLog(@"------frame3 = %@",NSStringFromCGRect(inster));
//    NSLog(@"------frame4 = %@",NSStringFromCGRect(rir2));
//    CAReplicatorLayer * replica = [CAReplicatorLayer layer];
//    replica.frame = inset;
//    replica.backgroundColor = [UIColor magentaColor].CGColor;
//
//    UIBezierPath * path2 = [UIBezierPath bezierPathWithOvalInRect:replica.frame];
//    path2.lineWidth = 10.0f;
//    path2.lineCapStyle = kCGLineCapRound;
    
    //遮罩层
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = self.bounds;
    _progressLayer.fillColor =  [[UIColor clearColor] CGColor];
    _progressLayer.strokeColor=[UIColor whiteColor].CGColor;
    _progressLayer.lineCap = kCALineCapSquare;
    _progressLayer.lineWidth = PROGRESS_LINE_WIDTH+length;
    _progressLayer.path = path.CGPath;
    

    const CGFloat *components = CGColorGetComponents(color1.CGColor);
    const CGFloat *components1 = CGColorGetComponents(color2.CGColor);
    float red = (components[0]+components1[0])/2;
    float green = (components[1]+components1[1])/2;
    float blue = (components[2]+components1[2])/2;
    UIColor *middleColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
    UIColor *minMiddleColor = [UIColor colorWithRed:components[0]/2+red/2 green:components[1]/2+green/2 blue:components[2]/2+blue/2 alpha:1];
    UIColor *maxMiddleColor = [UIColor colorWithRed:components1[0]/2+red/2 green:components1[1]/2+green/2 blue:components1[2]/2+blue/2 alpha:1];
    
    //渐变图层
    CALayer * grain = [CALayer layer];
    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(frame.size.width/2, 0, frame.size.width/2, frame.size.height/2);
    [gradientLayer setColors:[NSArray arrayWithObjects:(id)color1.CGColor,(id)minMiddleColor.CGColor, nil]];
    [gradientLayer setLocations:@[@0.3,@0.8, @1]];
    [gradientLayer setStartPoint:CGPointMake(0.05, 0)];
    [gradientLayer setEndPoint:CGPointMake(0, 1)];
    [grain addSublayer:gradientLayer];

    CAGradientLayer * gradientLayer1 = [CAGradientLayer layer];
    gradientLayer1.frame = CGRectMake(frame.size.width/2, frame.size.height/2, frame.size.width/2, frame.size.height/2);
    [gradientLayer1 setColors:[NSArray arrayWithObjects:(id)minMiddleColor.CGColor,(id)middleColor.CGColor, nil]];
    [gradientLayer1 setLocations:@[@0.3, @0.8,@1]];
    gradientLayer1.startPoint = CGPointMake(0, 0);
    gradientLayer1.endPoint = CGPointMake(0, 1);
    [grain addSublayer:gradientLayer1];
    
    CAGradientLayer *gradientLayer2 = [CAGradientLayer layer];
    gradientLayer2.frame = CGRectMake(0, frame.size.height/2.0, frame.size.width/2.0,  frame.size.height/2.0);
    [gradientLayer2 setColors:[NSArray arrayWithObjects:(id)middleColor.CGColor,(id)maxMiddleColor.CGColor, nil]];
    gradientLayer2.startPoint = CGPointMake(0.5, 1);
    gradientLayer2.endPoint = CGPointMake(0.5, 0);
    [grain addSublayer:gradientLayer2];
    
    CAGradientLayer *gradientLayer3 = [CAGradientLayer layer];
    gradientLayer3.frame = CGRectMake(0, 0, frame.size.width/2.0,  frame.size.height/2.0);
    [gradientLayer3 setColors:[NSArray arrayWithObjects:(id)maxMiddleColor.CGColor,(id)color2.CGColor, nil]];
    gradientLayer3.startPoint = CGPointMake(0.5, 1);
    gradientLayer3.endPoint = CGPointMake(0.5, 0);
    [grain addSublayer:gradientLayer3];
    //用progressLayer来截取渐变层 遮罩
    [grain setMask:_progressLayer];
    [self.layer addSublayer:grain];
    
    //增加动画
    CABasicAnimation *pathAnimation=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 1;
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnimation.fromValue=[NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue=[NSNumber numberWithFloat:1.0f];
    pathAnimation.autoreverses=NO;
    pathAnimation.repeatCount = 1;

    [_progressLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
}


- (void)gradentStartAngle:(float)start endAngle:(float)end startColor:(UIColor *)color1 endColor:(UIColor *)color2 {
    [self.layer removeAllAnimations];
    for (NSInteger i=self.layer.sublayers.count-1; i>=0; i--) {
        [self.layer.sublayers[i] removeFromSuperlayer];
    }
    
    CGSize size = self.frame.size;
    float pwidth = 8;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(size.width/2, size.height/2) radius:(size.width-pwidth)/2 startAngle:degressToRadius(start) endAngle:degressToRadius(end) clockwise:YES];
    //遮罩层
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = self.bounds;
    _progressLayer.fillColor =  [[UIColor clearColor] CGColor];
    _progressLayer.strokeColor=[UIColor whiteColor].CGColor;
    _progressLayer.lineWidth = pwidth;
    _progressLayer.path = path.CGPath;
    
    const CGFloat *components = CGColorGetComponents(color1.CGColor);
    const CGFloat *components1 = CGColorGetComponents(color2.CGColor);
    float red = (components[0]+components1[0])/2;
    float green = (components[1]+components1[1])/2;
    float blue = (components[2]+components1[2])/2;
    UIColor *middleColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
    UIColor *minMiddleColor = [UIColor colorWithRed:components[0]/2+red/2 green:components[1]/2+green/2 blue:components[2]/2+blue/2 alpha:1];
    UIColor *maxMiddleColor = [UIColor colorWithRed:components1[0]/2+red/2 green:components1[1]/2+green/2 blue:components1[2]/2+blue/2 alpha:1];
    
    //渐变图层
    CALayer * grain = [CALayer layer];
    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(size.width/2, 0, size.width/2, size.height/2);
    [gradientLayer setColors:[NSArray arrayWithObjects:(id)color1.CGColor,(id)minMiddleColor.CGColor, nil]];
    [gradientLayer setLocations:@[@0.3,@0.8, @1]];
    [gradientLayer setStartPoint:CGPointMake(0.05, 0)];
    [gradientLayer setEndPoint:CGPointMake(0, 1)];
    [grain addSublayer:gradientLayer];
    
    CAGradientLayer * gradientLayer1 = [CAGradientLayer layer];
    gradientLayer1.frame = CGRectMake(size.width/2, size.height/2, size.width/2, size.height/2);
    [gradientLayer1 setColors:[NSArray arrayWithObjects:(id)minMiddleColor.CGColor,(id)middleColor.CGColor, nil]];
    [gradientLayer1 setLocations:@[@0.3, @0.8]];
    gradientLayer1.startPoint = CGPointMake(0, 0);
    gradientLayer1.endPoint = CGPointMake(0, 1);
    [grain addSublayer:gradientLayer1];
    
    CAGradientLayer *gradientLayer2 = [CAGradientLayer layer];
    gradientLayer2.frame = CGRectMake(0, size.height/2.0, size.width/2.0, size.height/2.0);
    [gradientLayer2 setColors:[NSArray arrayWithObjects:(id)middleColor.CGColor,(id)maxMiddleColor.CGColor, nil]];
    gradientLayer2.startPoint = CGPointMake(0.5, 1);
    gradientLayer2.endPoint = CGPointMake(0.5, 0);
    [grain addSublayer:gradientLayer2];
    
    CAGradientLayer *gradientLayer3 = [CAGradientLayer layer];
    gradientLayer3.frame = CGRectMake(0, 0, size.width/2.0, size.height/2.0);
    [gradientLayer3 setColors:[NSArray arrayWithObjects:(id)maxMiddleColor.CGColor,(id)color2.CGColor, nil]];
    gradientLayer3.startPoint = CGPointMake(0.5, 1);
    gradientLayer3.endPoint = CGPointMake(0.5, 0);
    [grain addSublayer:gradientLayer3];
    //用progressLayer来截取渐变层 遮罩
    [grain setMask:_progressLayer];
    [self.layer addSublayer:grain];
    
    //增加动画
    CABasicAnimation *pathAnimation=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 1;
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnimation.fromValue=[NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue=[NSNumber numberWithFloat:1.0f];
    pathAnimation.autoreverses=NO;
    pathAnimation.repeatCount = 1;
    
    
    [_progressLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
}

@end
